package com.teamit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.teamit.entity.Employee;
import com.teamit.entity.IdeaComment;
import com.teamit.util.db.DBHelp;
import com.teamit.util.db.RowMapper;

public class IdeaCommentDao {

	DBHelp<IdeaComment> db = new DBHelp<IdeaComment>();
	
	public void save (IdeaComment ideaComment){
		String sql = "insert into t_ideacomment(id,content,createtime,employee_id,idea_id) values(?,?,?,?,?)";
		db.executeSQL(sql, ideaComment.getId(),ideaComment.getContent(),ideaComment.getCreatetime(),ideaComment.getEmployeeid(),ideaComment.getIdeaid());
	}
	
	public List<IdeaComment> findByIdeaId(String ideaid){
		String sql = "select ti.id,ti.content,ti.createtime,ti.employee_id,ti.idea_id,te.username from t_ideacomment as ti left join t_employee as te on ti.employee_id = te.id where ti.idea_id=?";
		return db.executeQueryToList(sql, new IdeaCommentRowMapper(), ideaid);
	}
	private class IdeaCommentRowMapper implements RowMapper<IdeaComment>{

		public IdeaComment mapRow(ResultSet rs) throws SQLException {

			IdeaComment ic = new IdeaComment();
			ic.setId(rs.getString("id"));
			ic.setContent(rs.getString("content"));
			ic.setCreatetime(rs.getString("createtime"));
			ic.setEmployeeid(rs.getString("employee_id"));
			ic.setIdeaid(rs.getString("idea_id"));
			
			Employee employee = new Employee();
			employee.setName(rs.getString("username"));
			ic.setEmployee(employee);
			
			return ic;
		}
		
	}
	public void del(String id) {

		String sql="delete from t_ideacomment where id=?";
		db.executeSQL(sql, id);
	}
	
}
